<template>
  <scroll-view scroll-y @scrolltolower="scrollToBottom">
    <!-- 已登录：显示购物车 -->
    <view v-if="memberStore.profile">
      <!-- 购物车列表 -->
      <view class="cart-list" v-if="true">
        <!-- 优惠提示 -->
        <view class="tips">
          <text class="label">满减</text>
          <text class="desc">满1件, 即可享受9折优惠</text>
        </view>
        <!-- 滑动操作分区 -->
        <uni-swipe-action>
          <!-- 滑动操作项 -->
          <uni-swipe-action-item v-for="item in cartList" :key="item.id" class="cart-swipe">
            <!-- 商品信息 -->
            <view class="goods">
              <text
                class="checkbox"
                :class="{ checked: item.selected }"
                @tap="checkBoxFn(item)"
              ></text>
              <navigator
                :url="`/pages/goods/goods?id=${item.id}`"
                hover-class="none"
                class="navigator"
              >
                <image mode="aspectFill" class="picture" :src="item.picture"></image>
                <view class="meta">
                  <view class="name ellipsis">{{ item.name }}</view>
                  <view class="attrsText ellipsis">{{ item.attrsText }}</view>
                  <view class="price">{{ item.price }}</view>
                </view>
              </navigator>
              <!-- 商品数量 -->
              <view class="count">
                <!-- <text class="text" @tap="minusFn(item)">-</text>
                <input class="input" type="number" v-model="item.count" />
                <text class="text" @tap="addFn(item)">+</text> -->
                <vk-data-input-number-box
                  v-model="item.count"
                  :min="1"
                  :max="item.stock"
                  :index="item.skuId"
                  @change="onChangeCount"
                />
              </view>
            </view>
            <!-- 右侧删除按钮 -->
            <template #right>
              <view class="cart-swipe-right">
                <button class="button delete-button" @tap="onDeleteCart(item.skuId)">删除</button>
              </view>
            </template>
          </uni-swipe-action-item>
        </uni-swipe-action>
      </view>
      <view class="toolbar">
        <text class="all" :class="{ checked: checkAll }" @tap="checkAllFn">全选</text>
        <text class="text">合计:</text>
        <text class="amount">{{ selectedCartListMoney }} </text>
        <view class="button-grounp">
          <view
            class="button payment-button"
            :class="{ disabled: selectedCartListCount.length === 0 }"
            @tap="gotoPayment"
          >
            去结算({{ selectedCartListCount }})
          </view>
        </view>
      </view>
    </view>
    <!-- 未登录：不显示购物车内容 -->
    <view class="login-blank" v-else>
      <text class="text">登录后可查看购物车中的商品</text>
      <navigator url="/pages/login/login">
        <button class="button">去登陆</button>
      </navigator>
    </view>
    <XtxGuess ref="XtxGuessRef"></XtxGuess>
  </scroll-view>
</template>

<script setup lang="ts">
import { ref, computed } from 'vue'
import { useGuessList } from '@/composables/index'
import { useMemberStore } from '@/stores/index'
import type { CartItem } from '@/types/cart'
import { onShow } from '@dcloudio/uni-app'
import {
  getMemberCartAPI,
  deleteMemberCartAPI,
  putMemberCartBySkuIdAPI,
  putMemberCartSelectedAPI,
} from '../request'
// sku的步进器组件
import type { InputNumberBoxEvent } from '@/components/vk-data-input-number-box/vk-data-input-number-box'
const { XtxGuessRef, scrollToBottom } = useGuessList()
const memberStore = useMemberStore()
const checkBox = ref(false)
// 获取购物车数据
const cartList = ref<CartItem[]>([])
// 选中单个复选框
const checkBoxFn = async (item: CartItem) => {
  item.selected = !item.selected
  await putMemberCartBySkuIdAPI(item.skuId, { selected: item.selected })
}
// 步进器
const onChangeCount = (ev: InputNumberBoxEvent) => {
  // 修改商品数量
  putMemberCartBySkuIdAPI(ev.index, { count: ev.value })
}
const getMemberCartData = async () => {
  const res = await getMemberCartAPI()
  cartList.value = res.result
  console.log(cartList.value, 'cartList.value')
}
// 点击删除按钮
const onDeleteCart = (skuId: string) => {
  // 弹窗二次确认
  uni.showModal({
    content: '是否删除',
    success: async (res) => {
      if (res.confirm) {
        // 后端删除单品
        await deleteMemberCartAPI({ ids: [skuId] })
        uni.showToast({ icon: 'success', title: '删除成功' })
        // 重新获取列表
        getMemberCartData()
      }
    },
  })
}
// 全选
const checkAll = computed(() => {
  return cartList.value.length && cartList.value.every((v) => v.selected)
})
// 计算用户选中的单品列表
let selectedCartList = computed(() => {
  return cartList.value.filter((item) => item.selected)
})
// 合计
const selectedCartListMoney = computed(() => {
  return selectedCartList.value.reduce((sum, item) => sum + item.nowPrice * item.count, 0)
})
// 结算
const selectedCartListCount = computed(() => {
  return selectedCartList.value.reduce((sum, item) => sum + item.count, 0)
})
// 全选按钮
const checkAllFn = () => {
  const _isSlected = !checkAll.value
  cartList.value.forEach((i) => {
    i.selected = _isSlected
  })
}
// 去结算
const gotoPayment = () => {
  if (selectedCartListCount.value === 0) {
    return uni.showToast({
      icon: 'none',
      title: '请选择商品',
    })
  }
  // 跳转到结算页
  uni.navigateTo({ url: '/pagesOrder/create/create' })
}
onShow(() => {
  if (memberStore.profile) {
    getMemberCartData()
  }
})
</script>
<style lang="scss">
page {
  background-color: #f7f7f8;
}
.login-blank {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-content: center;
  align-items: center;
  height: 60vh;
  .text {
    color: #444;
    font-size: 26rpx;
    margin: 20rpx 0;
  }
  .button {
    width: 240rpx !important;
    height: 60rpx;
    line-height: 60rpx;
    margin-top: 20rpx;
    font-size: 26rpx;
    border-radius: 60rpx;
    color: #fff;
    background-color: #27ba9b;
  }
}
// 购物车列表
.cart-list {
  padding: 0 20rpx;
  .tips {
    display: flex;
    align-items: center;
    line-height: 1;
    margin: 30rpx 10rpx;
    font-size: 26rpx;
    color: #666;

    .label {
      color: #fff;
      padding: 7rpx 15rpx 5rpx;
      border-radius: 4rpx;
      font-size: 24rpx;
      background-color: #27ba9b;
      margin-right: 10rpx;
    }
  }
}
.goods {
  display: flex;
  padding: 20rpx 20rpx 20rpx 80rpx;
  border-radius: 10rpx;
  background-color: #fff;
  position: relative;
}

.checkbox {
  position: absolute;
  top: 0;
  left: 0;
  text-align: center;
  align-items: center;
  justify-content: center;
  align-content: center;
  width: 80rpx;
  height: 200rpx;
  line-height: 200rpx;
  &::before {
    content: '\e6cd';
    font-family: 'erabbit' !important;
    font-size: 40rpx;
    color: #444;
  }
  &.checked::before {
    content: '\e6cc';
    color: #27ba9b;
  }
}

.picture {
  width: 170rpx;
  height: 170rpx;
}
.navigator {
  display: flex;
  justify-content: space-between;
}
.meta {
  flex: 1;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  margin-left: 20rpx;
}
.name {
  height: 72rpx;
  font-size: 26rpx;
  color: #444;
}
.attrsText {
  line-height: 1.8;
  padding: 0 15rpx;
  font-size: 24rpx;
  align-self: flex-start;
  border-radius: 4rpx;
  color: #888;
  background-color: #f7f7f8;
}

.price {
  line-height: 1;
  font-size: 26rpx;
  color: #444;
  margin-bottom: 2rpx;
  color: #cf4444;

  &::before {
    content: '￥';
    font-size: 80%;
  }
}
.count {
  position: absolute;
  bottom: 20rpx;
  right: 5rpx;

  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 220rpx;
  height: 48rpx;

  .text {
    height: 100%;
    padding: 0 20rpx;
    font-size: 32rpx;
    color: #444;
  }
  .input {
    height: 100%;
    text-align: center;
    border-radius: 4rpx;
    font-size: 24rpx;
    color: #444;
    background-color: #f6f6f6;
  }
}
// 吸底工具栏
.toolbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--window-bottom));
  z-index: 1;

  height: 100rpx;
  padding: 0 20rpx;
  display: flex;
  align-items: center;
  border-top: 1rpx solid #ededed;
  border-bottom: 1rpx solid #ededed;
  background-color: #fff;

  .all {
    margin-left: 25rpx;
    font-size: 14px;
    color: #444;
    display: flex;
    align-items: center;
  }

  .all::before {
    font-family: 'erabbit' !important;
    content: '\e6cd';
    font-size: 40rpx;
    margin-right: 8rpx;
  }

  .checked::before {
    content: '\e6cc';
    color: #27ba9b;
  }

  .text {
    margin-right: 8rpx;
    margin-left: 32rpx;
    color: #444;
    font-size: 14px;
  }

  .amount {
    font-size: 20px;
    color: #cf4444;

    .decimal {
      font-size: 12px;
    }

    &::before {
      content: '￥';
      font-size: 12px;
    }
  }

  .button-grounp {
    position: absolute;
    right: 10rpx;
    top: 50%;

    display: flex;
    justify-content: space-between;
    text-align: center;
    line-height: 72rpx;
    font-size: 13px;
    color: #fff;
    transform: translateY(-50%);

    .button {
      width: 240rpx;
      margin: 0 10rpx;
      border-radius: 72rpx;
    }

    .payment-button {
      background-color: #27ba9b;

      &.disabled {
        opacity: 0.6;
        background-color: #a2f8e6;
      }
    }
  }
}
.delete-button {
  border-radius: 0;
  height: 200rpx;
  background-color: rgb(220, 6, 6);
  color: white;
  line-height: 200rpx;
  color: white;
}
</style>
